var fs = require("fs");
var express = require("express");
var server = express();
var expressStatic = require("express-static");
var url = require("url");
var mysql = require("mysql");
var ejs = require("ejs")
var port = 8081;
var sql = mysql.createConnection({
	host:"localhost",
	user:"root",
	password:"admin",
	database:"movie"
})
sql.connect();

server.get("/",(request,reponse)=>{
	var hot = 0;
	var jijiang = 0;
	sql.query(`SELECT name,score,img,status FROM movie WHERE status="1"`,(error,data)=>{
		hot = data;
		sql.query(`SELECT name,score,img,status FROM movie WHERE status="0"`,(error,data)=>{
			jijiang = data;
			if(error){
				console.log(error)
				reponse.end("查找数据库失败!")
			}
			else{
				ejs.renderFile("static/index.html",{data:{hot:hot,jijiang:jijiang}},(error,result)=>{
					if(error){
						console.log(error)
						reponse.end("解析错误")
					}
					else{
						reponse.end(result)
					}

				})
			}
		})
	})
})
server.get("/film",(request,reponse)=>{
	sql.query(`SELECT * FROM movie`,(error,data)=>{
		if(error){
			console.log(error)
			reponse.end("查找数据库失败!")
		}
		else{
			ejs.renderFile("static/important/film.html",{data:data},(error,result)=>{
				if(error){
					console.log(error)
					reponse.end("解析错误")
				}
				else{
					reponse.end(result)
				}
			})
		}
	})
})


//服务器重启
setInterval(function(){
	sql.query(`SELECT * FROM movie`)
},720000);
server.use(expressStatic(__dirname + `/static`));
server.listen(port);
console.log(`server is runing at  ${port}` )
